package Servlets.lhl;/**
 * 项目名称：SaleOrderChangeServlet.java
 * 文件名称：SaleOrderChangeServlet.java
 * 类的描述：无
 * 创建时间：2025-07-25 11:00:35
 * 创建人：大帅比
 * <p>
 * 版权声明：本文件归 大帅比 所有，未经授权不得进行商业使用。
 */
 /*
                   _ooOoo_
                  o8888888o
                  88" . "88
                  (| -_- |)
                  O\  =  /O
               ____/`---'\____
             .'  \\|     |//  `.
            /  \\|||  :  |||//  \
           /  _||||| -:- |||||-  \
           |   | \\\  -  /// |   |
           | \_|  ''\---/''  |   |
           \  .-\__  `-`  ___/-. /
         ___`. .'  /--.--\  `. . __
      ."" '<  `.___\_<|>_/___.'  >'"".
     | | :  `- \`.;`\ _ /`;.`/ - ` : | |
     \  \ `-.   \_ __\ /__ _/   .-` /  /
======`-.____`-.___\_____/___.-`____.-'======
                   `=---='
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
            佛祖保佑       永无BUG
*/

import java.io.*;

import Util.GokJDBCUtil2;
import Util.RES;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@WebServlet(name = "SaleOrderChangeServlet", value = "/saleorderchange")
public class SaleOrderChangeServlet extends HttpServlet {
    public void init() {
    }
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
//        {id: '5', paymentstatus: '1', orderstatus: '0', remark: '白毫银针购买'}
        String id = req.getParameter("id");
        String paymentstatus = req.getParameter("paymentstatus");
        String orderstatus = req.getParameter("orderstatus");
        String remark = req.getParameter("remark");
        //如果都不为空
        if(id != null && paymentstatus != null && orderstatus != null && remark != null){
            //id转成整数,paymentstatus,orderstatus转成整数
            Integer id1 = Integer.parseInt(id);
            Integer paymentstatus1 = Integer.parseInt(paymentstatus);
            Integer orderstatus1 = Integer.parseInt(orderstatus);
            //如果paymentstatus1和orderstatus1不在0和1之间
            if((paymentstatus1 != 0 && paymentstatus1 != 1) || (orderstatus1 != 0 && orderstatus1 != 1)){
                //返回参数错误
                res.getWriter().write(RES.fail("参数错误"));
                return;
            }
            //sql语句
            String sql="update sales_order set paymentstatus=?,orderstatus=?,remark=? where id=?";
            GokJDBCUtil2.update(sql,paymentstatus1,orderstatus1,remark,id);
            //返回成功
            res.getWriter().write(RES.success("修改成功"));
        }else{
            res.getWriter().write(RES.fail("参数错误"));
        }
    }
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
        String method = req.getMethod();
        try {
            if (method.equals("POST")) {
                this.doPost(req, res);
            } else {
                PrintWriter writer = res.getWriter();
                writer.println("{\"code\":\"400\",\"msg\":\"此方法不允许\"}");
                writer.close();
            }
        } catch (Exception e) {
            log.error("服务器处理请求时发生错误", e);
            PrintWriter writer = res.getWriter();
            writer.println("{\"code\":\"500\",\"msg\":\"服务器处理请求时发生错误\"}");
            writer.close();
        }
    }

    public void destroy() {
    }
}